package controller;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import wfm.entities.User;

import com.web.util.RegisteredUsers;
import com.web.util.WebUtils;

@ManagedBean(name = "loginController")
@SessionScoped
public class LoginController {
	
	@ManagedProperty(value="#{css}")
	private CSSColorController css; 
	
    @ManagedProperty(value = "#{user}")
    private User user;
    
    @ManagedProperty(value = "false")
    private boolean showloginfailed;
    
    @ManagedProperty(value="isLoggedIn")
    private boolean isLoggedIn;
    
    
    private String loginName="b.mueller@realestate.com";
    private String loginPwd=null;
    
    
    /** Creates a new instance of LoginControl */
    public LoginController() {
       
    }
    
    @PostConstruct
    public void init(){
    	//getUser().setName("b.mueller@realestate.com");
    }
    
    
    public CSSColorController getCss() {
		return css;
	}



	public void setCss(CSSColorController css) {
		this.css = css;
	}



	public boolean getIsLoggedIn() {
		return isLoggedIn;
	}

	public void setIsLoggedIn(boolean isLoggedIn) {
		this.isLoggedIn = isLoggedIn;
	}

	public boolean getRenderLogin(){
		return !getIsLoggedIn();
	}
    
    public String reset() {
    	//complaintController = null;
        return "index";
    }

    public String login() {
        if (RegisteredUsers.isRegistered(loginName, loginPwd)) {
            setShowloginfailed(false);
            setIsLoggedIn(true);
            
            User user = RegisteredUsers.getUser(loginName,loginPwd);
            this.user.setName(user.getName());
            this.user.setPassword(user.getPassword());
            this.user.setFirstName(user.getFirstName());
            this.user.setLastName(user.getLastName());
            this.user.setUserGroup(user.getUserGroup());
            return showStartPage(user);
        } else {
            setShowloginfailed(true);
            FacesContext fc = FacesContext.getCurrentInstance();
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Password or User incorrect!", null);
		    fc.addMessage(null, msg);
		    fc.renderResponse();
            return "index";
        }        
    }
    
    public String showStartPage(User user){
    	if(User.GROUP_WORKER.equals(user.getUserGroup()))
    		return WebUtils.redirect("workerTasks");
    	else
    		return WebUtils.redirect("myTasks");
    }
    
	public String goToMainPage() {
		return WebUtils.redirect("myTasks");
	}
    
    public String logout(){
    	setIsLoggedIn(false);
    	user.setUserGroup("");
    	user.setFirstName("");
    	user.setLastName("");
    	user.setPassword("");
    	user.setName("");
    	return WebUtils.redirect("login");
    }
    

    /**
     * @return the showloginfailed
     */
    public boolean isShowloginfailed() {
        return showloginfailed;
    }

    /**
     * @param showloginfailed the showloginfailed to set
     */
    public void setShowloginfailed(boolean showloginfailed) {
        this.showloginfailed = showloginfailed;
    }

	public void setLoggedIn(boolean isLoggedIn) {
		this.isLoggedIn = isLoggedIn;
	}



	public String getLoginName() {
		return loginName;
	}



	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}



	public String getLoginPwd() {
		return loginPwd;
	}



	public void setLoginPwd(String loginPwd) {
		this.loginPwd = loginPwd;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
    
    
}

